//
//  SCHomeCell.m
//  Mutal
//
//  Created by TonyStart on 16/10/19.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCHomeCell.h"
#import "SCHomeLuckyBagViewModel.h"
#import <UIImageView+WebCache.h>
#import "NSString+SCTimeConvert.h"
#import "SCAWishController.h"
#import "SCWarmWishViewController.h"
#import "SCMainTabBar.h"
#import "SCLoginController.h"
#import "SCMyWishController.h"
@interface SCHomeCell()
@property (weak, nonatomic) IBOutlet UILabel *todayGetInfoLabel;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UILabel *todayGetLabel;
@property (weak, nonatomic) IBOutlet UILabel *todayLabel;
@property (weak, nonatomic) IBOutlet UILabel *allLabel;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
//愿望CELL 对应的控件
@property (weak, nonatomic) IBOutlet UIImageView *wishPic;
@property (weak, nonatomic) IBOutlet UILabel *wishUserNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *wishTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *wishAddTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *wishRewardNumLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *wishProgress;
@property (weak, nonatomic) IBOutlet UILabel *wishProgressNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *haveRewardPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *rewardNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalMoneyLabel;



@end
@implementation SCHomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

-(void)setLuckyBagModel:(SCHomeLuckBagModel *)luckyBagModel{
    _luckyBagModel = luckyBagModel;
    
    
    if (_luckyBagModel) {
        [self setupLucyBagContent];
    }
    
}
-(void)setWishModel:(SCWishModel *)wishModel{
    _wishModel = wishModel;
    
    if (_wishModel) {
        [self setupWishContent];
    }
}
//福包信息内容
-(void)setupLucyBagContent{
    NSDictionary *dict = self.luckyBagModel.info;
    //今日福包返现通知字符串
    NSString *luckyBagGetStr = [NSString stringWithFormat:@"%@ %@获得%@元返现",dict[@"user_nicename"],dict[@"mobile"],dict[@"coin"]];
    self.todayGetInfoLabel.text = luckyBagGetStr;
    //todayget
    self.todayGetLabel.text = [NSString stringWithFormat:@"%@/元",self.luckyBagModel.today_get];
    //progress百分比
    
    CGFloat todayGet = [self.luckyBagModel.today_get doubleValue];
    NSInteger todayAll = self.luckyBagModel.today;
 
    CGFloat progressValue = todayGet/todayAll;
    self.progressView.progress = progressValue;
    
    self.progressLabel.text = [NSString stringWithFormat:@"%0.1f%%",progressValue*100];
    
    self.todayLabel.text = [NSString stringWithFormat:@"总:%ld/元",self.luckyBagModel.today];
    
    self.allLabel.layer.borderColor = [UIColor redColor].CGColor;
    self.allLabel.layer.borderWidth = 1;
    self.allLabel.layer.cornerRadius = 2;
    self.allLabel.layer.masksToBounds = YES;
    
    //福包总额富文本

    NSString *allStr = [NSString stringWithFormat:@" %04ld ",self.luckyBagModel.all];
    NSMutableAttributedString *luckyBagAllMStr = [[NSMutableAttributedString alloc] init];
    
    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor],NSKernAttributeName: @(5)};
    NSDictionary *attrDict2 = @{NSKernAttributeName: @(2)};
    for (int i=0;i<allStr.length;i++) {
        NSString *indexStr = [NSString stringWithFormat:@"%c",[allStr characterAtIndex:i]];
        NSAttributedString *indexAStr = [[NSAttributedString alloc] initWithString:indexStr attributes:attrDict2];
        
        [luckyBagAllMStr appendAttributedString:indexAStr];
        
        
        if (i > 0 && i <allStr.length - 2) {
            
            NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"|" attributes:attrDict1];
            
            [luckyBagAllMStr appendAttributedString:str];
        }
        
    }

    self.allLabel.attributedText = luckyBagAllMStr;
    
}
//愿望列表内容

-(void)setupWishContent{
    NSString *picUrl = [NSString stringWithFormat:@"http://www.scxxhs.com/huzhu%@",self.wishModel.avatar];
  
    [self.wishPic sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:[UIImage imageNamed:@"默认头像"] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    self.wishUserNameLabel.text = self.wishModel.user_nicename;
    
    self.wishTitleLabel.text = self.wishModel.wish_title;
    
    self.wishAddTimeLabel.text =  [NSString getShowDateWithTime:self.wishModel.wish_add_time];
    
    self.wishRewardNumLabel.text = self.wishModel.wish_reward_num;
    
    CGFloat haveRewardPrice = round([self.wishModel.wish_reward_price doubleValue]);
    CGFloat totalPrice = [self.wishModel.wish_price doubleValue];
    
    
    CGFloat percentageValue = haveRewardPrice / totalPrice;
    self.wishProgress.progress = percentageValue;
    
    self.wishProgressNumberLabel.text = [NSString stringWithFormat:@"%0.0lf%%",percentageValue*100];

    self.haveRewardPriceLabel.text =[NSString stringWithFormat:@"已打赏¥%0.0lf",round([self.wishModel.wish_reward_price doubleValue])] ;
    
    self.rewardNumLabel.text = [NSString stringWithFormat:@"打赏人数%@",self.wishModel.wish_reward_num];

    self.totalMoneyLabel.text = [NSString stringWithFormat:@"求打赏%ld",[self.wishModel.wish_price integerValue]];
}
- (IBAction)luckyBagDetail:(id)sender {
}
- (IBAction)luckyBagClick:(id)sender {
    
}
//许个愿吧
- (IBAction)havaWIsh:(id)sender {
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_login"];
    
    if (!isLogin) {
        SCLoginController *loginVC = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil].instantiateInitialViewController;
        
        [[self getCurrentVC].navigationController pushViewController:loginVC animated:NO];
      
    }else{
        SCAWishController *aWishVC = [[SCAWishController alloc] init];
        
        [[self getCurrentVC].navigationController.childViewControllers.firstObject presentViewController:aWishVC animated:NO completion:^{
            
        }];
    }
    
}
//查看愿望
- (IBAction)lookWish:(id)sender {
    //1.通过响应链获得控制器
    //获取keywindow
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//    //取得根视图
//    UIView *frontView = window.subviews.firstObject;
//    //取得根视图的响应链
//    UIResponder *responder = frontView.nextResponder;
//    
//    if ([responder isKindOfClass:[SCMainTabBar class]]) {
//        SCMainTabBar *mainTabBar = (SCMainTabBar *)responder;
//        mainTabBar.selectedIndex = 1;
//        
//    }
    //2.通过 window的rootViewController获得根控制器
    UIViewController *rootVC=window.rootViewController;
    
    if ([rootVC isKindOfClass:[SCMainTabBar class]]) {
        SCMainTabBar *mainTabBarVC=(SCMainTabBar *)rootVC;
        mainTabBarVC.selectedIndex = 1;
    }
    
//    if (self.clickTT) {
//        self.clickTT();
//    }
    
}
//预热愿望
- (IBAction)warmupWish:(id)sender {
    SCWarmWishViewController *warmVC = [[SCWarmWishViewController alloc] init];
    warmVC.hidesBottomBarWhenPushed = YES;
    [[self getCurrentVC].navigationController pushViewController:warmVC animated:YES];
}
//我的愿望
- (IBAction)myWish:(id)sender {
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_login"];
    
    if (!isLogin) {
        SCLoginController *loginVC = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil].instantiateInitialViewController;
        
        [[self getCurrentVC].navigationController pushViewController:loginVC animated:NO];
        
    }else{
        SCMyWishController *myWishVC = [[SCMyWishController alloc] init];
        
        myWishVC.hidesBottomBarWhenPushed = YES;
        
        [[self getCurrentVC].navigationController pushViewController:myWishVC animated:YES];
    }
    
    
}
//获得当前正在显示的viewController
- (UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    //    如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
        result=nav.childViewControllers.lastObject;
        
    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }else{
        result = nextResponder;
    }
    
    return result;
}
@end
